package br.com.android.financas.activities;

import java.io.Serializable;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import br.com.android.financas.R;
import br.com.android.financas.adapter.DataAdapter;
import br.com.android.financas.helper.HomeHelper;
import br.com.android.financas.model.Poupanca;
import br.com.android.financas.watcher.CurrencyWatcher;

public class HomePoupanca extends HomeHelper<Poupanca> {

	private static final String CLICK_POUPANCA_ID = "id";

	private EditText campoValor;
	private Spinner campoData;
	private CheckBox campoDespesa;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.poupanca_screen);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		Intent intent = getIntent();
		campoData = (Spinner) findViewById(R.poupanca.data);
		campoValor = (EditText) findViewById(R.poupanca.valor);
		campoDespesa = (CheckBox) findViewById(R.poupanca.despesa);

		String data=(new DateTime()).toString("dd/MM/yyyy");
		if (intent.getExtras() != null
				&& intent.getExtras().get(CLICK_POUPANCA_ID) != null) {
			// get
			Integer id = (Integer) intent.getExtras().get(CLICK_POUPANCA_ID);
			setId(id);

			campoValor.setText(NumberFormat.getCurrencyInstance().format(getInstance().getValor()));
			data= (new DateTime(getInstance().getData().getTime())).toString("dd/MM/yyyy");

			campoDespesa.setChecked(getInstance().isDespesa());

		}

		List<String> datas=new ArrayList<String>();

		datas.add(data);

		DataAdapter dataAdapter= new DataAdapter(this, android.R.layout.simple_spinner_item, datas);
		dataAdapter.setDataSelecionada(new DateTime(getInstance().getData().getTime()));
		campoData.setAdapter(dataAdapter);
		campoValor.setRawInputType(Configuration.KEYBOARD_12KEY);

		campoValor.addTextChangedListener(new CurrencyWatcher(campoValor));
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.home_poupanca, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			getIntent().putExtra("changed", false);
			setResult(RESULT_CANCELED, getIntent());
			finish();
			return true;
		case R.id.menu_save:

			try {
				if(!isValid()){
					Toast.makeText(this, R.string.required_fields, Toast.LENGTH_LONG).show();
					return false;
				}

				if (getInstance().getId() == null) {
					save();
				} else {
					update();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

			getIntent().putExtra("changed", true);
			setResult(RESULT_OK, getIntent());
			finish();

			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public static void callMe(Context c, Serializable id) {
		Intent intent = new Intent(c, HomePoupanca.class);
		intent.putExtra(CLICK_POUPANCA_ID, id);
		c.startActivity(intent);
	}
}
